home *** CD-ROM | disk | FTP | other *** search
/ Aminet 51 / Aminet 51 (2002)(GTI - Schatztruhe)[!][Oct 2002].iso / Aminet / dev / c / TinyGL.lha / tinygl / src / gla.c < prev    next >
Encoding:
C/C++ Source or Header  |  2002-08-24  |  6.9 KB  |  304 lines

  1.  
  2. #include <GL/gla.h>
  3. #include "zgl.h"
  4.  
  5. static unsigned long *recup_cmap(int nc, unsigned long *cr);
  6.  
  7. typedef struct {
  8.     GLContext *gl_context;
  9.     int xsize,ysize;
  10.  
  11.     GLADrawable drawable;
  12.     unsigned char *image;
  13.     int bpp;
  14.    
  15.     //ave_pix_t *gc;
  16.  
  17.     // Ajouter la palette de couleur pour le rendu 8 bits (à utiliser avec rgb32).
  18.     // unsigned long *cmap;
  19.  
  20.  
  21.     /*
  22.     // Intent
  23.     int draw;
  24.  
  25.     // NGLX
  26.   int pixtype; // pixel type in TinyGL
  27.  
  28.     // GLX
  29.   Display *display;
  30.   XVisualInfo visual_info;
  31.   XImage *ximage;
  32.   GC gc;
  33.   Colormap cmap;
  34.   int do_convert; // true if must do convertion to X11 format
  35.   */
  36. } TinyGLAContext;
  37.  
  38.  
  39. /*
  40.  * CreateContext
  41.  */
  42. GLAContext glACreateContext() //Display *dpy, XVisualInfo *vis, GLAContext shareList, BOOL direct )
  43. {
  44.   TinyGLAContext *ctx;
  45.  
  46.   ctx=gl_malloc(sizeof(TinyGLAContext));
  47.     if (!ctx)
  48.       return NULL;
  49.   ctx->gl_context=NULL;
  50.     ctx->image=NULL;
  51.     ctx->drawable=NULL;
  52.  
  53.   return (GLAContext) ctx;
  54. }
  55.  
  56. /*
  57.  * DestroyContext
  58. * Supprimer la palette si elle est différente de NULL
  59.  */
  60. void glADestroyContext(GLAContext ctx1)
  61. {
  62.     TinyGLAContext *ctx = (TinyGLAContext *) ctx1;
  63.   if (ctx->gl_context != NULL) {
  64.     glClose();
  65.   }
  66.   gl_free(ctx);
  67. }
  68.  
  69. /* resize the glx viewport : we try to use the xsize and ysize
  70. given. We return the effective size which is guaranted to be smaller */
  71.  
  72. static int glA_resize_viewport(GLContext *c, int *xsize_ptr, int *ysize_ptr)
  73. {
  74.     TinyGLAContext *ctx;
  75.     int xsize,ysize;
  76.  
  77.     ctx=(TinyGLAContext *)c->opaque;
  78.  
  79.     xsize=*xsize_ptr;
  80.     ysize=*ysize_ptr;
  81.  
  82.     /* we ensure that xsize and ysize are multiples of 2 for the zbuffer.
  83. TODO: find a better solution */
  84.     xsize&=~3;
  85.     ysize&=~3;
  86.  
  87.     if (xsize == 0 || ysize == 0) return -1;
  88.  
  89.     *xsize_ptr=xsize;
  90.     *ysize_ptr=ysize;
  91.                                                     
  92.     if (ctx->image == NULL){
  93.         ctx->image = (unsigned char *)gl_malloc(xsize * ysize);
  94.     }else{
  95.         if ((xsize !=    ctx->xsize) || (ysize != ctx->ysize)){
  96.             gl_free(ctx->image);
  97.             ctx->image = (unsigned char *)gl_malloc(xsize * ysize);
  98.         }
  99.     }
  100.  
  101.     ctx->xsize=xsize;
  102.     ctx->ysize=ysize;
  103.     
  104.     if (ctx->image == NULL){
  105.         return -1;
  106.     }
  107.    
  108.     /* resize the Z buffer */
  109.     ZB_resize(c->zb,NULL,xsize,ysize);
  110.     
  111.     return 0;
  112. }
  113.  
  114. /*
  115.  * MakeCurrent
  116.  */
  117. /* we assume here that drawable is a window */
  118. int glAMakeCurrent(GLADrawable drawable, GLAContext ctx1)
  119. {
  120.   TinyGLAContext *ctx = (TinyGLAContext *) ctx1;
  121.   int xsize,ysize;
  122.   ZBuffer *zb;
  123.   unsigned int palette[ZB_NB_COLORS];
  124.   unsigned char color_indexes[ZB_NB_COLORS];
  125.     unsigned long *new_palette;
  126.     int i;
  127.     int mode;
  128.                                                                                         
  129.     int bpp;
  130.    
  131.     //if((dpy==NULL)||(ctx1==NULL)) return(0);
  132.     
  133.     bpp = GetBitMapAttr(drawable->RPort->BitMap, BMA_DEPTH);
  134.     ctx->bpp = bpp;
  135.    
  136.   if (ctx->gl_context == NULL) {
  137.  
  138.     /* create the TinyGL context */
  139.     // Affectation du drawable
  140.  
  141.         //ctx->display=dpy;
  142.         ctx->drawable=drawable;
  143.      
  144.         xsize = drawable->Width;
  145.         ysize = drawable->Height;
  146.  
  147.     /* ximage structure */
  148.         //ctx->ximage=NULL;
  149.         //ctx->shm_use=0; /* no shm */
  150.  
  151.         // La partie qui suit équivaut à la fonction locale init() de igl.c
  152.         /*
  153.     {
  154.         int mode, bpp;
  155.  
  156.                 // RGB 16/24/32
  157.         bpp = bits_per_pixel(ctx->display,&ctx->visual_info);
  158.  
  159.         //mode = ZB_MODE_RGB24;
  160.                 // currently, we only support 16 bit rendering
  161.           //mode = ZB_MODE_5R6G5B; // nglx ne supporte que le mode 16 bits
  162.                                             
  163.         */
  164.         mode = ZB_MODE_INDEX; //5R6G5B;
  165.                         
  166.         if (mode == ZB_MODE_INDEX){
  167.  
  168.         //if (attr.depth == 8) {
  169.       /* get the colormap from the window */
  170.             /*
  171.       ctx->cmap = attr.colormap;
  172.  
  173.       if ( XAllocColorCells(ctx->display,ctx->cmap,True,&tmp_plane,0,
  174.                             pixel,ZB_NB_COLORS) == 0) {
  175.                 // private cmap
  176.         ctx->cmap = XCreateColormap(ctx->display, drawable,
  177.                                     ctx->visual_info.visual, AllocAll);
  178.         XSetWindowColormap(ctx->display, drawable, ctx->cmap);
  179.         for(i=0;i<ZB_NB_COLORS;i++) pixel[i]=i;
  180.       }
  181.             */
  182.  
  183.       for(i=0;i<ZB_NB_COLORS;i++){
  184.                 color_indexes[i]=i;
  185.       }
  186.  
  187.       /* Open the Z Buffer - 256 colors */
  188.       zb=ZB_open(xsize,ysize,ZB_MODE_INDEX,ZB_NB_COLORS,color_indexes,palette,NULL);
  189.       if (zb == NULL) {
  190.         fprintf(stderr, "Error while initializing Z buffer\n");
  191.         exit(1);
  192.       }
  193.  
  194.       // Affectation de la cmap au drawable
  195.  
  196.             new_palette = recup_cmap(ZB_NB_COLORS, (unsigned long *)palette);
  197.             LoadRGB32(&drawable->WScreen->ViewPort, new_palette);
  198.             free(new_palette);
  199.  
  200.             // construction de la cmap du contexte
  201.             //
  202.             //for (i=0; i<ZB_NB_COLORS; i++) {
  203.             //    xcolor.flags = DoRed | DoGreen | DoBlue;
  204.             //
  205.             //    xcolor.red = (palette[i]>>8) & 0xFF00;
  206.             //    xcolor.green = (palette[i] & 0xFF00);
  207.             //    xcolor.blue = (palette[i] << 8) & 0xFF00;
  208.             //    xcolor.pixel = pixel[i];
  209.             //    XStoreColor(ctx->display,ctx->cmap,&xcolor);
  210.             //}
  211.             //ctx->do_convert = 1;
  212.         }else{
  213.             // mode 16 bits
  214.             zb = ZB_open(xsize, ysize, mode, 0, NULL, NULL, NULL);
  215.         }
  216.  
  217.         //}
  218.  
  219.     /* initialisation of the TinyGL interpreter */
  220.     glInit(zb);
  221.  
  222.     ctx->gl_context=gl_get_context();
  223.     ctx->gl_context->opaque=(void *) ctx;
  224.     ctx->gl_context->gl_resize_viewport=glA_resize_viewport; //igl_resize_viewport
  225.  
  226.     /* set the viewport : we force a call to glX_resize_viewport */
  227.     ctx->gl_context->viewport.xsize=-1;
  228.     ctx->gl_context->viewport.ysize=-1;
  229.  
  230.         glViewport(0, 0, xsize, ysize);
  231.   }
  232.  
  233.   return 1;
  234. }
  235.  
  236. /*
  237.  * SwapBuffers
  238.  */
  239. void glASwapBuffers(GLADrawable drawable)
  240. {
  241.   GLContext *gl_context;
  242.   TinyGLAContext *ctx;
  243.     int i;
  244.     int comp = 0;
  245.     unsigned char *ptr;
  246.     unsigned long *new_palette;
  247.     unsigned short *seize;
  248.     unsigned short s;
  249.     unsigned char *buf;
  250.     unsigned char r,g,b;
  251.    
  252.   /* retrieve the current GLXContext */
  253.   gl_context=gl_get_context();
  254.   ctx=(TinyGLAContext *)gl_context->opaque;
  255.  
  256.     if (ctx->bpp == 8){
  257.         ZB_copyFrameBuffer(ctx->gl_context->zb, ctx->image, ctx->xsize);
  258.         
  259.         WriteChunkyPixels(drawable->RPort, 0, 0, drawable->Width - 1, drawable->Height - 1, (unsigned char *)ctx->image/*ctx->gl_context->zb->pbuf*/, drawable->Width);
  260.     }else{
  261.         printf("gla.c: cybergfx bpp = %d\n", ctx->bpp);
  262.         
  263.         ZB_copyFrameBuffer(ctx->gl_context->zb, ctx->image, ctx->xsize);
  264.  
  265.         (void)WritePixelArray(ctx->image,
  266.                                                 0,0,
  267.                                                 drawable->Width*ctx->bpp/8,
  268.                                                 drawable->RPort,
  269.                                                 0, 0,
  270.                                                 drawable->Width,
  271.                                                 drawable->Height,
  272.                                                 RECTFMT_RGB);
  273.     }
  274. }
  275.  
  276. /* Create an AmigaOS color map (usable with LoadRGB32()) */
  277. static unsigned long *recup_cmap(int nc, unsigned long *cr)
  278. {
  279.     int c;
  280.     unsigned long *colourtable;
  281.     unsigned char r, g, b;
  282.  
  283.     if ((colourtable = (unsigned long *)malloc((1 + 3 * nc + 1) * sizeof(unsigned long))))
  284.     {
  285.         colourtable[0] = (nc << 16) + 0;
  286.  
  287.         for (c = 0; c < nc; c++)
  288.         {
  289.             r = (unsigned char)((cr[c]>>16) & 0xFF);
  290.             g = (unsigned char)((cr[c]>>8) & 0xFF);
  291.             b = (unsigned char)((cr[c]) & 0xFF);
  292.        
  293.             colourtable[3 * c + 1] = (unsigned long)(r * 0x01010101);
  294.             colourtable[3 * c + 2] = (unsigned long)(g * 0x01010101);
  295.             colourtable[3 * c + 3] = (unsigned long)(b * 0x01010101);
  296.         }
  297.  
  298.         colourtable[1 + 3 * nc] = 0;
  299.     }
  300.  
  301.     return colourtable;
  302. }
  303.  
  304.